<template>
  <!--系统管理中技术标准-->
  <div class="methodsAddcon" v-if="addVisible">
    <div class="twoStageWindow">
      <div class="twoStageWindowTitle">
        {{newOrEdit=='new'?'新增检测方法':(newOrEdit=='edit'?'编辑检测方法':(newOrEdit=='look' || newOrEdit=='review'?'查看检测方法':(newOrEdit=='copy'?'复制检测方法':"生成新版本")))}}
        <div class="twoStageWindowClose" v-if="newOrEdit != 'review'">
          <el-button type="primary" @click="changeStatus" size="mini" v-if="isNewViserion && $parent.userOpernation.version.display "
            :disabled="!isRepeat">生成新版本</el-button>
          <el-button type="primary" @click="subMethods('commit')" size="mini" :disabled="!isRepeat" v-if="isSubmit">提 交</el-button>
          <el-button type="primary" @click="subMethods('save')" size="mini" v-if="isSave" :disabled="!isRepeat">保 存</el-button>
          <!-- 启用后管理员编辑修改现场检测 -->
          <!-- <el-button v-if="$parent.userOpernation.openUpdate.display && isStatus == 1 && newOrEdit == 'edit'" type="primary" size="mini" style="float:right;" @click="updateIsspot">提交</el-button> -->
          <el-button size="mini" @click="close('methodsForm')">取 消</el-button>
        </div>
        <!--<div class="twoStageWindowClose" v-if="newOrEdit == 'look'">
              <el-button size="mini" @click="close('methodsForm')">取 消</el-button>
            </div>-->
        <div class="twoStageWindowClose" v-if="newOrEdit == 'review'">
          <el-button type="primary" @click="reviewFlowPop" size="mini">审核</el-button>
          <el-button size="mini" @click="close('methodsForm')">取 消</el-button>
        </div>
      </div>
      <div class="containercontent" style="margin-top:40px;height:calc(100% - 40px);padding-top: 0;">
        <div class="form">
          <el-form label-width="110px" :inline="false" class="demo-form-inline abilityForm" :model="methodsForm" :rules="methodsRules"
            ref="methodsForm">
            <el-form-item label="方法类型" prop='methodType'>
              <el-col :span="22">
                <el-select v-model="methodsForm.methodType" placeholder="请选择" style="width: 100%;" size="mini"
                  :disabled="!isDisabled">
                  <el-option v-for="item in methodTypeSelect.itemList" :key="item.code" :label="item.name" :value="item.code">
                  </el-option>
                </el-select>
              </el-col>
            </el-form-item>
            <el-form-item label="方法名称" prop='name'>
              <el-col :span="22">
                <el-input size="mini" placeholder="中文名称+编号" v-model="methodsForm.name" :title="methodsForm.name" style="overflow-y:visible;"
                  :disabled="!isDisabled"></el-input>
              </el-col>
            </el-form-item>
            <el-form-item label="现场检测" prop='isSpot'>
              <el-col :span="22">
                <el-select v-model="methodsForm.isSpot" placeholder="请选择" style="width: 100%;" size="mini" :disabled="!isEditSpot"
                  @change="updateIsspot">
                  <el-option v-for="item in yesorno" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </el-col>
            </el-form-item>
            <el-form-item label="版本" prop='version' v-if="newOrEdit != 'new' && newOrEdit != 'newVersion'">
              <el-col :span="22">
                <el-input size="mini" placeholder="版本号" v-model="methodsForm.version" disabled></el-input>
              </el-col>
            </el-form-item>
            <el-form-item label="分组" prop='group'>
              <el-col :span="22">
                <el-select v-model="methodsForm.group" placeholder="请选择" style="width: 100%;" size="mini" :disabled="!isDisabled">
                  <el-option v-for="item in groupSelect.itemList" :key="item.code" :label="item.name" :value="item.code">
                  </el-option>
                </el-select>
              </el-col>
            </el-form-item>
            <el-form-item label="状态" prop='status' v-if="newOrEdit != 'new' && newOrEdit != 'newVersion'">
              <el-col :span="22">
                <el-select v-model="methodsForm.status" placeholder="请选择" style="width: 100%;" size="mini" disabled>
                  <el-option v-for="item in enableStatus" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </el-col>
            </el-form-item>
          </el-form>
        </div>
        <div class="elTab">
          <el-tabs v-model="activeName" style="width: 100%;">
            <el-tab-pane label="检测条件" name="first">
              <el-button v-if="isAccord" type="primary" size="mini" style="float:right;margin-bottom:5px;" @click="tianjiajianceyiju">新增</el-button>
              <el-button v-if="$parent.userOpernation.openUpdate.display && isStatus == 1 && newOrEdit == 'edit'" type="primary"
                size="mini" style="float:right;margin-bottom:5px;margin-right:5px;" @click="addMethodAccord">保存</el-button>

              <el-table :data="methodsForm.accordBeanList" style="width: 100%" class="tableCon" size="mini" height="calc(100% - 33px)">
                <el-table-column label="序号" min-width="50px">
                  <template slot-scope="scope">
                    {{scope.$index+1}}
                  </template>
                </el-table-column>
                <el-table-column label="分析条件" property="name" min-width="100px">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.name" size="mini" placeholder="请输入内容" :disabled="!isAccord"></el-input>

                  </template>
                </el-table-column>
                <el-table-column label="分析条件内容" property="content" min-width="100px">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.content" size="mini" placeholder="请输入内容" :disabled="!isAccord"></el-input>

                  </template>
                </el-table-column>

                <el-table-column label="操作" min-width="100px" v-if="isAccord">
                  <template slot-scope="scope">
                    <el-button @click="deletejianceyiju(scope.$index)" type="text" size="mini">删除</el-button>
                  </template>
                </el-table-column>
              </el-table>

            </el-tab-pane>
            <el-tab-pane label="分析项" name="second">
              <el-button type="primary" size="mini" @click="addLittleAnalysis" v-if="isItemAnas" style="float:right;margin-bottom:5px">添加分析项</el-button>

              <el-table :data="methodsForm.analysisBeanList" style="width: 100%" class="tableCon" size="mini" height="calc(100% - 33px)">
                <el-table-column label="序号" fixed min-width="50px">
                  <template slot-scope="scope">
                    {{scope.$index+1}}
                  </template>
                </el-table-column>
                <el-table-column property="name" label="分析项名称" fixed min-width="180px">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.name" size="mini" placeholder="请输入内容" :disabled="!canEdit" :title="scope.row.name"></el-input>
                  </template>
                </el-table-column>
                <el-table-column property="type" label="类型" min-width="150px">
                  <template slot-scope="scope">
                    <el-select v-model="scope.row.type" size="mini" :disabled="!canEdit" @change="changeType($event,scope.row)">
                      <el-option v-for="item in typeSelect" :key="item.code" :label="item.name" :value="item.code">
                      </el-option>
                    </el-select>
                  </template>
                </el-table-column>
                <el-table-column property="dataType" label="数据类型" min-width="150px">
                  <template slot-scope="scope">
                    <el-select v-model="scope.row.dataType" size="mini" :disabled="!canEdit || !scope.row.isDisable">
                      <el-option v-for="item in dataSelect.itemList" :key="item.code" :label="item.name" :value="item.code">
                      </el-option>
                    </el-select>
                  </template>
                </el-table-column>
                <el-table-column property="unit" label="单位" min-width="100px">
                  <template slot-scope="scope">
                    <el-select v-model="scope.row.unit" size="mini" filterable :disabled="!canEdit">
                      <el-option v-for="item in allUnit" :key="item.code" :label="item.ename" :value="item.code"
                        :disabled="item.status == 0">
                      </el-option>
                    </el-select>
                  </template>
                </el-table-column>
                <el-table-column property="formula" label="公式" min-width="100px">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.formula" size="mini" placeholder="请输入内容" style="display:none"></el-input>
                    <el-button type="text" size="mini" @click="itemFormulaInfo(scope.row,'analysis')" :disabled="scope.row.id == undefined || scope.row.dataType =='DT002' || newOrEdit == 'copy' || newOrEdit == 'newVersion'">查看详情</el-button>
                  </template>
                </el-table-column>
                <el-table-column property="decimalRule" label="修约规则" min-width="100px">
                  <template slot-scope="scope">
                    <el-select v-model="scope.row.decimalRule" size="mini" :disabled="!canEdit" clearable>
                      <el-option v-for="item in ruleSelect.itemList" :key="item.code" :label="item.name" :value="item.code">
                      </el-option>
                    </el-select>
                  </template>
                </el-table-column>
                <el-table-column property="decimalNumber" label="小数点后位数" min-width="100px">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.decimalNumber" placeholder="小数点后保留位数" size="mini" :min="0" :disabled="!canEdit"
                      @blur="decNumber(scope.row)" class="deal" oninput="this.value=this.value.replace(/[^0-9]+/,'');"></el-input>
                  </template>
                </el-table-column>
                <el-table-column property="defaultValue" label="默认值" min-width="100px">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.defaultValue" size="mini" :disabled="!canEdit || !scope.row.isDisable"></el-input>
                  </template>
                </el-table-column>
                <el-table-column label="操作" fixed='right' min-width="100px" v-if="isItemAnas">
                  <template slot-scope="scope">
                    <el-button type="text" size="mini" @click="deleteanalysis(scope.$index)">删除</el-button>

                  </template>
                </el-table-column>
              </el-table>

            </el-tab-pane>
            <el-tab-pane label="检测项目" name="third">
              <el-button type="primary" size="mini" @click="addItemAnalysis" v-if="isItemAnas" style="float:right;margin-bottom:5px">添加检测项目</el-button>
              <el-table :data="methodsForm.itemBeanList" style="width: 100%" size="mini" class="tableCon" height="calc(100% - 33px)">
                <el-table-column label="序号" fixed min-width="50px">
                  <template slot-scope="scope">
                    {{scope.$index+1}}
                  </template>
                </el-table-column>
                <el-table-column property="item" label="检测项目" fixed min-width="200px">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.itemName" size="mini" placeholder="请输入内容" disabled v-if="!canEdit"></el-input>
                    <el-select @focus="loadItems($event,scope.row)" v-model="scope.row.itemName" size="mini" :disabled="!canEdit"
                      v-if="canEdit" value-key="id" filterable remote :remote-method="remoteMethod" :loading="loadingSelect">
                      <el-option v-if="scope.row.loadItem" v-for="item in itemList" :key="item.id" :label="item.name"
                        :value="item" :disabled="item.disabled">
                      </el-option>
                    </el-select>
                  </template>
                </el-table-column>
                <el-table-column label="报告字" property="reportWord" min-width="200px">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.reportWord" size="mini" placeholder="请输入内容" :disabled="!canEdit"></el-input>
                  </template>
                </el-table-column>
                <el-table-column property="formula" label="公式" min-width="100px">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.formula" size="mini" placeholder="请输入内容" style="display:none"></el-input>
                    <el-button type="text" size="mini" @click="itemFormulaInfo(scope.row,'item')" :disabled="scope.row.id == undefined || scope.row.dataType =='DT002' || newOrEdit == 'copy' || newOrEdit == 'newVersion' || typeof scope.row.itemName == 'object'">查看详情</el-button>
                  </template>
                </el-table-column>
                <el-table-column property="dataType" label="数据类型" min-width="100px">
                  <template slot-scope="scope">
                    <el-select v-model="scope.row.dataType" size="mini" :disabled="!canEdit">
                      <el-option v-for="item in dataSelect.itemList" :key="item.code" :label="item.name" :value="item.code">
                      </el-option>
                    </el-select>
                  </template>
                </el-table-column>
                <el-table-column property="high" label="测定上限" min-width="100px">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.high" size="mini" placeholder="请输入内容" :disabled="!canEdit" class="deal"
                      oninput="this.value=this.value.replace(/[^0-9.]+/,'');"></el-input>

                  </template>
                </el-table-column>
                <el-table-column property="low" label="测定下限" min-width="100px">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.low" size="mini" placeholder="请输入内容" :disabled="!canEdit" class="deal"
                      oninput="this.value=this.value.replace(/[^0-9.]+/,'');"></el-input>
                  </template>
                </el-table-column>
                <el-table-column property="detectionLimit" label="方法检出限" min-width="100px">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.detectionLimit" size="mini" placeholder="请输入内容" :disabled="!canEdit"></el-input>
                  </template>
                </el-table-column>
                <el-table-column property="unit" label="单位" min-width="110px">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.unitName" size="mini" placeholder="请输入内容" disabled v-if="!canEdit"></el-input>
                    <el-select @focus="loadUnits($event,scope.row)" v-model="scope.row.unitName" size="mini" :disabled="!canEdit"
                      v-if="canEdit" value-key="code" filterable remote :remote-method="remoteUnit" :loading="loadingUnit">
                      <el-option v-if="scope.row.loadUnit" v-for="item in unitList" :key="item.code" :label="item.ename"
                        :value="item">
                      </el-option>
                    </el-select>
                  </template>
                </el-table-column>

                <el-table-column property="decimalRule" label="修约规则" min-width="100px">
                  <template slot-scope="scope">
                    <el-select v-model="scope.row.decimalRule" size="mini" :disabled="!canEdit" clearable>
                      <el-option v-for="item in ruleSelect.itemList" :key="item.code" :label="item.name" :value="item.code">
                      </el-option>
                    </el-select>
                  </template>
                </el-table-column>
                <el-table-column property="decimalNumber" label="小数点后位数" min-width="100px">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.decimalNumber" placeholder="小数点后保留位数" size="mini" :min="0" @blur="decNumber(scope.row)"
                      :disabled="!canEdit" class="deal" oninput="this.value=this.value.replace(/[^0-9]+/,'');"></el-input>
                  </template>
                </el-table-column>
                <el-table-column label="操作" fixed='right' min-width="100px" v-if="isItemAnas">
                  <template slot-scope="scope">
                    <el-button type="text" size="mini" @click="deleteitemBeanList(scope.$index)">删除</el-button>

                  </template>
                </el-table-column>
              </el-table>

            </el-tab-pane>
            <el-tab-pane label="方法附件" name="fourth" v-if="newOrEdit !='new' && newOrEdit !='newVersion'&& newOrEdit !='copy' ">

              <!--accept=".xlsx,.xls"-->
              <form id="fileForm">
                <input type="file" name="file" id="file" @change="upload" multiple>
              </form>
              <el-button v-if="isItemAnas && $parent.userOpernation.upload.display" @click="uploadClick" type="primary"
                size="mini" style="float:right;margin-bottom:5px">上传</el-button>

              <el-table :data="methodsForm.attachmentBeanList" style="width: 100%" size="mini" height="calc(100% - 33px)">
                <el-table-column label="序号" min-width="50px">
                  <template slot-scope="scope">
                    {{scope.$index+1}}
                  </template>
                </el-table-column>
                <el-table-column property="fileName" label="附件名称" min-width="200px" :show-overflow-tooltip="true">
                </el-table-column>
                <el-table-column property="fileSize" label="附件大小" min-width="100px">
                  <template slot-scope="scope">
                    {{scope.row.fileSize == 0 ? 1 : scope.row.fileSize }} KB
                  </template>
                </el-table-column>
                <el-table-column property="createByName" label="上传人" min-width="80px">
                </el-table-column>
                <el-table-column property="createTime" label="上传时间" min-width="100px" :formatter="timeFormatter">
                </el-table-column>
                <el-table-column label="操作" min-width="100px">
                  <template slot-scope="scope">
                    <el-button type="text" size="mini" @click="deleteattachment(scope.row)" v-if="isItemAnas">删除</el-button>
                    <el-button type="text" size="mini" @click="downloadattachment(scope.row)" v-if="$parent.userOpernation.download.display">下载</el-button>
                  </template>
                </el-table-column>

              </el-table>

            </el-tab-pane>
            <el-tab-pane label="审核信息" name="five" v-if="(newOrEdit == 'look') && isStatus != 2 && (methodsForm.source == '' || methodsForm.source == null || methodsForm.source == undefined )">
              <div id="myIframe" v-if="reload" v-loading="loading1" style="text-align:center;">
                <span style="color:#909399;line-height:100px;" v-if="WorkFlowMsg != '' && WorkFlowMsg != undefined">{{WorkFlowMsg}}</span>
                <iframe :src="url" id="workFlowIframe" ref="workFlowIframe" width="100%" height="100%" frameborder="0"
                  scrolling="auto"></iframe>
              </div>
            </el-tab-pane>
          </el-tabs>
        </div>

      </div>
      <div class="user-footer" v-if="newOrEdit == 'look'">
        <span>

          创建人:{{methodsForm.createBy}}

        </span>
        <span>

          创建时间:{{methodsForm.createTime}}

        </span>
        <span v-if="methodsForm.updateBy != undefined">

          修改人：{{methodsForm.updateBy}}

        </span>
        <span v-if="methodsForm.updateTime != undefined">

          修改时间：{{methodsForm.updateTime}}

        </span>
      </div>
    </div>
    <add-formula :addVisible.sync="addFormulaShow" :methodsForm="methodsForm" :allItem="allItem" :rowInfo="rowInfo"
      :itemOrAnalysis="itemOrAnalysis" :newOrEdit="newOrEdit"></add-formula>
    <review-flow :addVisible.sync="reviewFlowShow" :reviewData="reviewData" :procInstType="procInstType" :reviewTitle="reviewTitle"
      @reloadMain="readMethod"></review-flow>
  </div>
</template>

<script>
  import {
    mapState
  } from 'vuex'
  const addFormula = () => import('./formulaInfo.vue');
  const reviewFlow = () =>
    import('../../../../common/reviewFlow.vue');

  export default {
    data() {
      return {
        reload: true,
        popupMask: true,
        methodsForm: {
          methodType: '',
          name: '',
          group: '',
          submitType: '',
          version: '',
          isSpot: false,
          accordBeanList: [],
          analysisBeanList: [],
          attachmentBeanList: [],
          itemBeanList: [],
        },
        methodsRules: {
          methodType: [{
            required: true,
            message: '请选择类型',
            trigger: 'change'
          }, ],
          name: [{
            required: true,
            type: 'string',
            message: '请填写名称',
            trigger: 'blur'
          }, ],
          // version:[
          // 		{required: true,type: 'string', message: '请填写版本号', trigger: 'blur' },
          // 	],
          group: [{
            required: true,
            message: '请选择分组',
            trigger: 'change'
          }, ],
          isSpot: [{
            required: true,
            message: '请选择现场检测',
            trigger: 'change'
          }, ]

        },
        groupValue: '',
        dictionaryList: [],
        activeName: 'first',
        allItem: [], //所有检测项目
        allUnit: [], //所有单位
        beforeallItem: [],
        itemName: '',
        loading: false,
        yesornoValue: '1',
        enableStatusValue: '1',
        methodTypeValue: '',
        analysisItemTableDateValue: '',
        isStatus: 0,
        addFormulaShow: false, //公式弹框
        rowInfo: null, //点击的检测项或分析项的行信息
        itemOrAnalysis: null, //查看公式，分析项还是检测项
        isRepeat: true,
        reviewFlowShow: false, //审核流显示
        reviewData: null, //点击行需要审核的信息
        procInstType: 't_method', //审核流要查的表
        reviewTitle: '检测方法',
        isSave: false, //保存按钮显示和隐藏,新增和生成新版本，状态为草稿时出现
        isSubmit: true, //提交按钮 只有查看时不出现
        isDisabled: true, //编辑状态
        isNewViserion: false, //新版本按钮
        isItemAnas: false, //检测项和分析项的添加按钮,只有新增,生成新版本,草稿状态下编辑可出现
        isFoulmal: true, //公式是否可编辑
        isAccord: true, // 检测条件的编辑状态
        breakFlag: 1, //是否中断提交
        isHaveFoul: false, //检测项和分析项是否有公式
        isEditSpot: false, //管理员启用状态可编辑现场检测
        loading1: true,
        url: "",
        WorkFlowMsg: "",
        itemList: [],
        loadingSelect: false,
        loadingUnit: false,
        unitList: [],

      }
    },
    computed: {
      ...mapState(['enableStatus1', 'groupSelect', 'yesorno', 'enableStatus', 'mainEquipType', 'dataSelect',
        'unitTableDate', 'ruleSelect', 'methodTypeSelect', 'submitType', 'typeSelect'
      ]),
    },
    props: ['newOrEdit', 'canEdit', 'status', 'methodsDetail', 'addVisible'],
    components: {
      addFormula,
      reviewFlow
    },
    mounted() {
      // this.getMethod();
      this.$store.dispatch('dogetDictionary');

    },
    watch: {
      methodsDetail() {
        for (var i in this.methodsDetail) { //赋值
          this.methodsForm[i] = this.methodsDetail[i]

        }
        if (this.methodsForm.createTime != undefined) {
          this.methodsForm.createTime = this.methodsForm.createTime.slice(0, this.methodsForm.createTime.indexOf("."));
        }
        if (this.methodsForm.updateTime != undefined) {
          this.methodsForm.updateTime = this.methodsForm.updateTime.slice(0, this.methodsForm.updateTime.indexOf("."));
        }
        for (var i in this.methodsForm.analysisBeanList) {
          if (this.methodsForm.analysisBeanList[i].isDisable == undefined) {
            this.methodsForm.analysisBeanList[i].isDisable = true;
          }
          if (this.methodsForm.analysisBeanList[i].type == 1) {
            this.methodsForm.analysisBeanList[i].isDisable = false;
          }
        }
        this.isStatus = this.methodsForm.status;

        this.showOpers()
      },
      addVisible(val, old) {
        if (val) {
          this.readAllUnit();
          this.readAllItem('');
          this.showOpers()
          this.getFilePath()
        }
      },
      activeName(val) {
        if (val == "five") {
          if (process.env.NODE_ENV == 'development') {
            this.url = "../../static/workflow.html?id=" + this.methodsForm.id + "&procInstType=" + this.procInstType +
              "&fromModel=look"

          } else {
            this.url = "static/workflow.html?id=" + this.methodsForm.id + "&procInstType=" + this.procInstType +
              "&fromModel=look"

          }
          var that = this;
          // setTimeout(function() {
          //     let iframe = document.getElementById("workFlowIframe");
          window.addEventListener('message', that.handleMessage)
          // }, 3000)
        }
      }

    },
    methods: {
      remoteMethod(query) {
        if (query !== '') {
          this.loadingSelect = true;
          setTimeout(() => {
            this.loadingSelect = false;
            this.itemList = this.allItem.filter(item => {
              return item.name.toLowerCase()
                .indexOf(query.toLowerCase()) > -1;
            });
          }, 100);
        } else {
          this.itemList = [];
        }
      },
      remoteUnit(query) {
        if (query !== '') {
          this.loadingUnit = true;
          setTimeout(() => {
            this.loadingUnit = false;
            this.unitList = this.allUnit.filter(item => {
              return item.ename.toLowerCase()
                .indexOf(query.toLowerCase()) > -1;
            });
          }, 200);
        } else {
          this.unitList = [];
        }
      },
      // 验证保留位数只能为整数
      decNumber(row) {
        // var reg = /^\d+$/;
        var reg = /^([0-9]?|10)$/
        var isInt = reg.test(Number(row.decimalNumber));
        if (!isInt) {
          row.decimalNumber = null;
        }
      },
      timeFormatter(row, column, cellValue, index) {
        return this.util.formatterDate({
          cellValue: cellValue
        })
      },
      showOpers() { //控制按钮和输入框状态
        if (this.newOrEdit == 'new' || this.newOrEdit == 'copy') { //新增进入
          this.isSave = true;
          this.isSubmit = true;
          this.isNewViserion = false; //新版本按钮
          this.isDisabled = true;
          this.isItemAnas = true;
          this.isFoulmal = false;
          this.isAccord = true;
          this.isEditSpot = true;

        }
        if (this.newOrEdit == 'newVersion') { //新版本进入
          this.isSave = false;
          this.isSubmit = true;
          this.isDisabled = false;
          this.isNewViserion = false; //新版本按钮
          this.isItemAnas = true;
          this.isFoulmal = false;
          this.isAccord = true;
          this.isEditSpot = true;
        }
        if (this.newOrEdit == 'edit') { //编辑进入
          // 启用状态下 管理员进入
          if (this.$parent.userOpernation.openUpdate.display && this.isStatus == 1) {
            this.isSave = false;
            this.isSubmit = false;
            this.isDisabled = false; //部分可以编辑，
            this.isNewViserion = true; //新版本按钮出现
            this.isItemAnas = false;
            this.isFoulmal = false;
            this.isAccord = true;
            this.isEditSpot = true;
            this.$emit('update:canEdit', false)
          }
          if (!this.$parent.userOpernation.openUpdate.display && this.isStatus == 1) {
            this.isSave = false;
            this.isSubmit = false;
            this.isDisabled = false; //部分可以编辑，
            this.isNewViserion = true; //新版本按钮出现
            this.isItemAnas = false;
            this.isFoulmal = false;
            this.isAccord = false;
            this.isEditSpot = false;
            this.$emit('update:canEdit', false)
          }
          if (this.methodsForm.source != undefined && this.methodsForm.source != null && this.methodsForm.source !=
            '') { //如果是从中心库生成的  不可生成新版本
            this.isNewViserion = false; //新版本按钮出现
          }

          if (this.isStatus == 2) { //草稿状态进入的编辑界面
            this.isSave = true;
            this.isSubmit = true;
            this.isNewViserion = false;
            this.isDisabled = true;
            this.isItemAnas = true;
            this.isFoulmal = true;
            this.isAccord = true;
            this.isEditSpot = true;
          }
          if (this.isStatus == 4) { //驳回状态进入的编辑界面
            this.isSave = true;
            this.isSubmit = true;
            this.isNewViserion = false;
            this.isDisabled = true;
            this.isItemAnas = true;
            this.isFoulmal = true;
            this.isAccord = true;
            this.isEditSpot = true;
          }
        }
        if (this.newOrEdit == 'look' || this.newOrEdit == 'review') { //详情进入
          this.isSave = false;
          this.isSubmit = false;
          this.isNewViserion = false; //所有不可编辑
          this.isDisabled = false;
          this.isItemAnas = false;
          this.isFoulmal = false;
          this.isAccord = false;
          this.isEditSpot = false;
        }
      },
      //重载数据
      reLoad() {
        this.reload = false;
        this.$nextTick(() => {
          this.reload = true;
        })
      },

      //关闭
      close(formName) {
        this.$refs.methodsForm.resetFields();
        this.$emit('update:addVisible', false);
        this.isRepeat = true;
        this.methodsForm = {
          methodType: '',
          name: '',
          group: '',
          submitType: '',
          version: '',
          accordBeanList: [],
          analysisBeanList: [],
          attachmentBeanList: [],
          itemBeanList: [],
          isSpot: false,

        };
        this.activeName = "first";
        this.isSave = false; //保存按钮显示和隐藏,新增和生成新版本，状态为草稿时出现
        this.isSubmit = true; //提交按钮 只有查看时不出现
        this.isDisabled = true; //编辑状态
        this.isNewViserion = false; //新版本按钮
        this.isItemAnas = false; //检测项和分析项的添加按钮,只有新增,生成新版本,草稿状态下编辑可出现
        this.isFoulmal = true;
        this.breakFlag = 1;
        this.isHaveFoul = false;
        this.WorkFlowMsg = "";
        this.loading1 = true;
        this.isEditSpot = false;
        this.itemList = [];
        this.unitList = [];

      },
      // 验证不为空
      isNotNull(str, msg) {
        if (str == "" || str == undefined) {
          this.$notify({
            type: 'warning',
            title: '警告',
            message: msg + "不能为空！"
          })
          this.breakFlag = 0;
        } else {
          this.breakFlag = this.breakFlag * 1;
          return str;
        }
      },
      changeStatus() {
        this.$confirm('是否生成新版本', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.methodsForm.attachmentBeanList.length = 0;
          this.$emit('update:addVisible', false)

          this.$nextTick(() => {
            this.$emit('update:canEdit', true)
            this.$emit('update:newOrEdit', 'newVersion')
            this.$emit('update:addVisible', true)
            this.activeName = "first"
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消生成新版本'
          });
        });
      },
      // 提交编辑添加
      subMethods(newVersion) {
        this.$refs.methodsForm.validate((valid) => { //表单验证
          if (valid) {
            this.breakFlag = 1;
            // 处理方法附件
            var attachmentBeanListItem = {};
            var newattAchmentBeanList = [];
            for (var i = 0; i < this.methodsForm.attachmentBeanList.length; i++) {
              //  attachmentBeanListItem.attachment = this.methodsForm.attachmentBeanList[i].attachment;
              attachmentBeanListItem.remark = this.methodsForm.attachmentBeanList[i].remark;
              newattAchmentBeanList.push(attachmentBeanListItem);
            }
            //处理检测条件数据

            var newAccordBean = [];
            for (var i = 0; i < this.methodsForm.accordBeanList.length; i++) {
              var accordBeanListItem = {};
              accordBeanListItem.name = this.methodsForm.accordBeanList[i].name;
              accordBeanListItem.content = this.methodsForm.accordBeanList[i].content;
              accordBeanListItem.remark = this.methodsForm.accordBeanList[i].remark;

              newAccordBean.push(accordBeanListItem);
            }
            //处理分析项数据

            var newanalysisBeanList = [];
            for (var i = 0; i < this.methodsForm.analysisBeanList.length; i++) {

              var analysisBeanListItem = {};
              for (var j in this.allUnit) {
                if (this.methodsForm.analysisBeanList[i].unit == this.allUnit[j].code) {
                  this.methodsForm.analysisBeanList[i].unitName = this.allUnit[j].ename
                }
              }
              analysisBeanListItem.name = this.methodsForm.analysisBeanList[i].name;
              analysisBeanListItem.type = this.methodsForm.analysisBeanList[i].type;
              analysisBeanListItem.dataType = this.methodsForm.analysisBeanList[i].dataType;
              analysisBeanListItem.unit = this.isNotNull(this.methodsForm.analysisBeanList[i].unit, '分析项的单位');
              analysisBeanListItem.unitName = this.methodsForm.analysisBeanList[i].unitName
              analysisBeanListItem.decimalRule = this.methodsForm.analysisBeanList[i].decimalRule;
              analysisBeanListItem.decimalNumber = this.methodsForm.analysisBeanList[i].decimalNumber;
              analysisBeanListItem.defaultValue = this.methodsForm.analysisBeanList[i].defaultValue;
              analysisBeanListItem.remark = this.methodsForm.analysisBeanList[i].remark;
              analysisBeanListItem.code = this.methodsForm.analysisBeanList[i].code;
              analysisBeanListItem.formula = this.methodsForm.analysisBeanList[i].formula; //公式
              if (this.methodsForm.analysisBeanList[i].formula != '' && this.methodsForm.analysisBeanList[i].formula !=
                undefined && this.methodsForm.analysisBeanList[i].formula != null) {
                this.isHaveFoul = true
              }
              analysisBeanListItem.formulaAlias = this.methodsForm.analysisBeanList[i].formulaAlias;
              if (this.methodsForm.analysisBeanList[i].dataType == "DT002") {
                analysisBeanListItem.formula = null;
                analysisBeanListItem.formulaAlias = null;
              }
              newanalysisBeanList.push(analysisBeanListItem);
            }
            //处理检测项目数据

            var newitemBeanList = [];
            for (var i = 0; i < this.methodsForm.itemBeanList.length; i++) {
              var itemBeanListItem = {};
              // for (var j in this.allUnit) {
              //   if (this.methodsForm.itemBeanList[i].unit == this.allUnit[j].code) {
              //     this.methodsForm.itemBeanList[i].unitName = this.allUnit[j].ename
              //   }
              // }
              if (this.methodsForm.itemBeanList[i].unitName != undefined && this.methodsForm.itemBeanList[i].unitName
                .hasOwnProperty('code')) {
                this.methodsForm.itemBeanList[i].unit = this.methodsForm.itemBeanList[i].unitName.code
                this.methodsForm.itemBeanList[i].unitName = this.methodsForm.itemBeanList[i].unitName.ename
              }

              if (this.methodsForm.itemBeanList[i].itemName != undefined && this.methodsForm.itemBeanList[i].itemName
                .id != undefined) { //如果改变了检测项目值 itemName是个对象 则传id和code
                itemBeanListItem.item = this.methodsForm.itemBeanList[i].itemName.id;
                itemBeanListItem.itemCode = this.methodsForm.itemBeanList[i].itemName.code;
              } else if (this.methodsForm.itemBeanList[i].itemName == undefined && (this.methodsForm.itemBeanList[
                    i]
                  .item == undefined || this.methodsForm.itemBeanList[i].item == '')) {
                this.$notify({
                  type: 'warning',
                  title: "警告",
                  message: '检测项目名称不能空'
                })
                return
              } else { //如果没有改 itemName是检测项目的名字
                itemBeanListItem.item = this.methodsForm.itemBeanList[i].item;
                itemBeanListItem.itemCode = this.methodsForm.itemBeanList[i].itemCode;
              }
              itemBeanListItem.unit = this.isNotNull(this.methodsForm.itemBeanList[i].unit, '检测项的单位');
              itemBeanListItem.unitName = this.methodsForm.itemBeanList[i].unitName;
              itemBeanListItem.dataType = this.methodsForm.itemBeanList[i].dataType;
              itemBeanListItem.reportWord = this.isNotNull(this.methodsForm.itemBeanList[i].reportWord, '报告字');
              itemBeanListItem.decimalRule = this.methodsForm.itemBeanList[i].decimalRule;
              itemBeanListItem.decimalNumber = this.methodsForm.itemBeanList[i].decimalNumber;
              itemBeanListItem.high = this.methodsForm.itemBeanList[i].high;
              itemBeanListItem.equalHigh = this.methodsForm.itemBeanList[i].equalHigh;
              itemBeanListItem.low = this.methodsForm.itemBeanList[i].low;
              itemBeanListItem.equalLow = this.methodsForm.itemBeanList[i].equalLow;
              itemBeanListItem.detectionLimit = this.methodsForm.itemBeanList[i].detectionLimit;
              itemBeanListItem.formula = this.methodsForm.itemBeanList[i].formula; //公式
              if (this.methodsForm.itemBeanList[i].formula != '' && this.methodsForm.itemBeanList[i].formula !=
                undefined && this.methodsForm.itemBeanList[i].formula != null) {
                this.isHaveFoul = true
              }
              itemBeanListItem.formulaAlias = this.methodsForm.itemBeanList[i].formulaAlias;
              if (this.methodsForm.itemBeanList[i].dataType == "DT002") {
                itemBeanListItem.formula = null;
                itemBeanListItem.formulaAlias = null;
              }
              newitemBeanList.push(itemBeanListItem);
            }
            var data = {
              name: this.methodsForm.name,
              methodType: this.methodsForm.methodType,
              group: this.methodsForm.group,
              isSpot: this.methodsForm.isSpot,
              version: this.methodsForm.version,
              status: this.methodsForm.status,
              accordBeanList: newAccordBean,
              analysisBeanList: newanalysisBeanList,
              attachmentBeanList: newattAchmentBeanList,
              itemBeanList: newitemBeanList,

            };
            console.log(this.breakFlag)
            if (this.breakFlag == 0) {
              return;
            }

            // submitType 0代表保存，1代表提交
            if (this.newOrEdit == 'new' || this.newOrEdit == 'copy') {
              var url = '/technology/addMethod.htm';
              data.parentVersion = null;
              data.id = null;
            } else if (this.newOrEdit == 'edit') {
              var url = '/technology/updateMethod.htm';
              data.id = this.methodsForm.id

            } else if (this.newOrEdit == 'newVersion') {
              var url = '/technology/addMethod.htm';
              data.id = this.methodsForm.id;
              data.parentVersion = this.methodsForm.parentVersion;
            }
            var msg = "";
            if (newVersion == 'save') {
              var msg = "保存成功";
              data.submitType = this.submitType.submitDraft
            } else if (newVersion == 'commit') {
              msg = "提交成功";
              data.submitType = this.submitType.submitOpen
            }
            this.isRepeat = false;
            this.$http({
              method: 'post',
              apiUrl: url,
              params: data,
              isJson: true,
              isLoading:true
            }).then(res => {
              this.isRepeat = true;
              if (res.code == 0) {
                if ((this.newOrEdit == 'copy' || this.newOrEdit == 'newVersion') && this.isHaveFoul) {
                  msg = "提交成功，公式已被清空，请重新设置公式！"
                }
                this.$notify({
                  type: 'success',
                  message: msg,
                  title: "成功"
                });
                if (newVersion == 'save') {
                  var row = {
                    id: res.data
                  }
                  this.$parent.editMethods(row, 'edit')
                  this.$parent.readMethods(this.$parent.MethodsQuery);

                } else if (newVersion == 'commit') {
                  this.$parent.readMethods(this.$parent.MethodsQuery);
                  this.close('methodsForm');
                }


              }

            }).catch(() => {

            });

          } else {
            return false;
          }
        });
      },
      // 修改启用后现场检测
      updateIsspot() {
        if (this.$parent.userOpernation.openUpdate.display && this.isStatus == 1 && this.newOrEdit == 'edit') {
          this.$http({
            method: 'post',
            apiUrl: '/technology/updMethodSpot.htm',
            params: {
              id: this.methodsForm.id,
              isSpot: this.methodsForm.isSpot,
            },
            isJson: true
          }).then(res => {
            if (res.code == 0) {
              this.$notify({
                type: "success",
                title: "成功",
                message: "修改成功！"
              })
            }
          })
        }
      },
      // 添加编辑弹框 搜索检测项目
      readAllItem(item) {
        this.allItem = [];
        this.$http({
          method: 'post',
          apiUrl: '/technology/readAllItem.htm',
          params: {
            name: ""
          },
          isJson: true
        }).then(res => {
          if (res.code == 0) {

            this.allItem = res.data;
          }

        })
      },
      // 添加公式弹框
      itemFormulaInfo(row, itemOrAnalysis) {
        this.rowInfo = row;
        this.itemOrAnalysis = itemOrAnalysis;
        this.addFormulaShow = true;

      },
      readAllUnit(item) {
        this.allUnit = [];
        this.$http({
          method: 'post',
          apiUrl: '/technology/readLabUnitList.htm',
          params: {
            ename: item
          },
          isJson: true
        }).then(res => {
          if (res.code == 0) {

            this.allUnit = res.data;
            // console.log(this.allUnit)
          }

        })
      },
      loadItems(val, row) {
        this.$set(row, 'loadItem', true)
      },
      loadUnits(val, row) {
        this.$set(row, 'loadUnit', true)
      },
      changeType(val, row) { //如果分析项中类型的值为曲线分析项,数据类型为数字型 输入框不可编辑

        if (val == 1) { //如果是曲线分析项,数据类型为数字型
          row.dataType = "DT001"
          // row.disable = false
          this.$set(row, 'isDisable', false)
        } else {
          this.$set(row, 'isDisable', true)
        }
      },
      //
      addMethodAccord() {
        var data = {
          accordBeanList: this.methodsForm.accordBeanList,
          id: this.methodsForm.id
        }
        this.$http({
          method: 'post',
          apiUrl: '/technology/addMethodAccord.htm',
          params: data,
          isJson: true
        }).then(res => {
          if (res.code == 0) {
            this.$notify({
              type: 'success',
              message: '修改检测条件成功!',
              title: "成功"
            })
          }

        })
      },
      //添加检测条件
      tianjiajianceyiju() {
        this.methodsForm.accordBeanList.push({
          "name": '',
          "content": "",
          "remark": "",
          "canEdit": true
        })

      },
      //添加分析项
      addLittleAnalysis() {
        this.methodsForm.analysisBeanList.push({
          "name": "",
          "type": 0,
          "dataType": "DT001",
          "unit": "",
          "decimalRule": "",
          "decimalNumber": "",
          "defaultValue": null,
          "remark": "",
          "canEdit": true,
          "isDisable": true,
        })
      },
      //添加检测项目
      addItemAnalysis() {
        this.methodsForm.itemBeanList.push({
          "item": "",
          "reportWord": "",
          "unit": "",
          "dataType": "DT001",
          "decimalRule": "",
          "decimalNumber": "",
          "high": null,
          "equalHigh": null,
          "low": null,
          "equalLow": null,
          "detectionLimit": "",
          "canEdit": true

        })
      },

      //删除检测条件
      deletejianceyiju(index) {
        this.methodsForm.accordBeanList.splice(index, 1);
      },
      //删除分析项
      deleteanalysis(index) {
        this.methodsForm.analysisBeanList.splice(index, 1);
      },
      //删除检测项目
      deleteitemBeanList(index) {
        this.methodsForm.itemBeanList.splice(index, 1);
      },
      // 模拟file的点击事件
      uploadClick() {
        document.getElementById("file").click()
      },
      // 上传文件
      upload(event) {
        var formData = new FormData();
        var files = document.getElementById('file').files
        for (var i of files) {
          formData.append('file', i)
        }

        formData.append('ids', this.methodsDetail.id)
        formData.append('type', 3)
        formData.append('taskType', null)
        formData.append('detectability', null)
        formData.append('fileType', 'methodTechnology')
        this.$axios({
          method: 'post',
          url: '/download/fileUpload.htm',
          data: formData,
          headers: {
            'Content-Type': 'multipart/form-data',
            'token': sessionStorage.getItem('loginAccount') ? sessionStorage.getItem('loginAccount') : undefined,
          }
        }).then(res => {
          var data = res.data
          event.target.value = "";
          if (data.code == 0) {
            this.$notify({
              title: '消息',
              message: '上传成功',
              type: 'success'
            })
            this.getFilePath();
          } else {
            this.$notify({
              title: '消息',
              message: '上传失败',
              type: "warning"
            })
          }

        })
      },
      //删除方法附件
      deleteattachment(row) {
        this.$confirm('是否删除', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            method: 'post',
            apiUrl: '/download/fileDelete.htm',
            params: {
              id: row.id,
            },
            isJson: false,
          }).then(res => {
            if (res.code === 0) {
              this.$message({
                type: 'success',
                message: '删除成功!'
              });
              this.getFilePath()
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      // 下载附件
      downloadattachment(row) {
        var url = "/download/fileDownload.htm"
        let token = sessionStorage.getItem('loginAccount') ? sessionStorage.getItem('loginAccount') : undefined
        window.open(process.env.baseUrl + url + '?id=' + row.id + "&token=" + token);
      },
      // getFilePathByIds
      getFilePath() {
        this.$http({
          method: 'post',
          apiUrl: '/download/getFilePathByIds.htm',
          params: {
            ids: this.methodsDetail.id
          },
          isJson: false,
        }).then(res => {
          if (res.code == 0) {
            this.methodsForm.attachmentBeanList = res.data
          }
        })
      },
      // 审核流弹框
      reviewFlowPop() {
        this.reviewFlowShow = true;
        this.reviewData = this.methodsDetail
      },
      // 关闭弹框，刷新页面
      readMethod() {
        this.$store.dispatch('getWorkflowList', 0).then(res => {
          this.$notify({
            showClose: true,
            message: '操作成功',
            type: 'success',
            title: "成功"
          })
        })
        this.close('methodsForm');
        this.$parent.readMethods(this.$parent.MethodsQuery);

      },
      handleMessage(event) {
        const data = event.data
        switch (data.cmd) {
          case 'loadingHide':
            this.loading1 = false;
            this.WorkFlowMsg = "";
            break
          case 'errorMsg':
            this.loading1 = false;
            this.WorkFlowMsg = "没有查询到审核信息"
            break;
        }
      },

    },

  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->

<style>
  .methodsAddcon .elTab .el-tabs .el-tabs__content {
    height: calc(100% - 55px)
  }

  .methodsAddcon .elTab .el-tabs .el-tab-pane {
    height: 100%;
  }

  .methodsAddcon .elTab .el-tabs .el-table {
    height: calc(100% - 40px);
  }

  .methodsAddcon .user-footer {
    height: 35px;
    line-height: 35px;
  }

  .el-cascader-menu {
    min-width: 200px;
  }

  .methodUpload .el-upload {
    float: right;
  }

  #file {
    width: 50px;
    height: 20px;
    opacity: 0;
    position: absolute;
    left: 0;
    top: 0;
    z-index: -1;
  }
</style>
<style scoped>
  .el-form .el-form-item {
    display: inline-block;
    width: 32%;
  }

  .el-col-22 .el-select {
    width: 100%;
  }

  .el-form.demo-form-inline {
    padding-top: 15px
  }

  .abilityForm .el-form-item {
    margin-bottom: 10px !important;
  }

  .form {
    height: 150px;
  }

  .elTab {
    height: calc(100% - 150px);
    padding: 0 20px;
  }

  .elTab .el-tabs {
    height: 100%;
  }

  .elTab .el-tabs .width100 {
    height: 100%;
  }
</style>
